package cn.crm.service;

import cn.crm.entiy.User;

import java.util.ArrayList;
import java.util.List;

public class UserServiceImpl implements UserService {

    private List<User> userList;

    public UserServiceImpl() {
        this.userList = new ArrayList<User>(){
            {
                add(new User("user1","123","张三"));
                add(new User("user2","123","李四"));
            }
        };
    }

    /**
     * 登陆
     * @param username 用户名
     * @param password 密码
     * @return 如果登陆成功则返回用户，否则返回null
     */
    @Override
    public User login(String username, String password) {
        //循环判断用户是否存在
        for (User user:this.userList) {
            //如果用户名一致，则去比较密码
            if(username.equals(user.getUsername())){
                if(password.equals(user.getPassword())){
                    return user;
                }else{
                    System.out.println("用户名或密码错误");
                    return null;
                }
            }
        }

        System.out.println("用户不存在");
        return null;
    }
}
